package com.cbs.android.function.imagecrop.sample;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.cbs.android.component.application.activity.CBSActivity;
import com.cbs.android.component.graphicsutils.GraphicsUtils;
import com.cbs.android.component.toast.Toast;
import com.cbs.android.function.imagecrop.ImageCropFunction;
import com.cbs.android.function.imagecrop.ImageCropHandler;
import com.cbs.android.function.imagepick.ImagePickFunction;
import com.cbs.android.function.imagepick.ImagePickHandler;

import java.util.List;

public class MainActivity extends CBSActivity {
    private static final String TAG = MainActivity.class.getName();
    private ImageView resultView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.init(this);
        setContentView(R.layout.activity_main);

        resultView = (ImageView) findViewById(R.id.result);
    }

    public void crop(View view) {
        ImagePickFunction.start(this, new ImagePickHandler() {
            @Override
            public void onPickSuccess(int i, Intent intent, List<Uri> list) {
//                Crop.of(list.get(0), Uri.fromFile(new File(getExternalCacheDir(), "111"))).withAspect(1, 1).start(MainActivity.this);
                ImageCropFunction.start(MainActivity.this, list.get(0), 2, 1, 800, 400, new ImageCropHandler() {
                    @Override
                    public void onCropSuccess(int requestCode, Intent intent, Uri result) {
                        resultView.setImageBitmap(GraphicsUtils.loadBitmap(result.getPath()));
                    }

                    @Override
                    public void onCancel(int i, Intent intent) {
                    }
                });
            }

            @Override
            public void onCancel(int i, Intent intent) {

            }
        });
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
//        super.onActivityResult(requestCode, resultCode, result);
//        if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
//            resultView.setImageBitmap(GraphicsUtils.loadBitmap(new File(getExternalCacheDir(), "111").getPath()));
//        }
//    }

    public void pick(View view) {
        ImagePickFunction.start(this, new ImagePickHandler() {
            @Override
            public void onPickSuccess(int i, Intent intent, List<Uri> list) {
                resultView.setImageBitmap(BitmapFactory.decodeFile(list.get(0).getPath()));
            }

            @Override
            public void onCancel(int i, Intent intent) {

            }
        });
    }
}
